package com.montran.search;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.exceptions.FilterException;
import com.exceptions.ResourcesException;
import com.montran.read.resources.UtilPropertyFiles;

public class SearchDate implements Search {
	private Date dateToFind;
	private Date dateSource;
	private Integer operation;
	private DateFormat formatter;

	public SearchDate( String nameResource, String dateFormat,
			Integer operation) {
		this.operation = operation;
		this.formatter = new SimpleDateFormat(dateFormat);
		fillData(nameResource);
	}

	public SearchDate(Integer operation, Date dateToFind ) {
		this.operation = operation;
		this.dateToFind = dateToFind;
		
	}

	@Override
	public Boolean find() throws FilterException {
		Boolean result = false;
		if (operation == 1 && dateToFind.equals(dateSource)) {
			result = true;
		}
		if (operation == 2 && dateToFind.before(dateSource)) {
			result = true;
		}
		if (operation == 3 && dateToFind.after(dateSource)) {
			result = true;
		}
		return result;

	}

	@Override
	public void fillData(String nameResource) {
		try {
			String resourceWords = new String();
			resourceWords = UtilPropertyFiles.readPropertiesFile(nameResource);
			dateToFind = (Date) formatter.parse(resourceWords);
		} catch (ResourcesException e) {
			e.toString();
		} catch (ParseException e) {
			e.toString();
		}
	}
	
	@Override
	public void setDataSource(Object data) {
		this.dateSource = (Date) data;
	}
	
	
}
